//
//  AnyObserverAndBinderViewController.swift
//  test-ios
//
//  Created by wyz5204 on 2023/5/5.
//

import UIKit
import CocoaLumberjack
import RxSwift

class AnyObserverAndBinderViewController: MVVMBaseViewController<AnyObserverAndBinderViewModel> {
    
    private lazy var linearLayout = {
        return UIStackView().then { view in
            view.axis = .vertical
            view.distribution = .fillEqually
            view.alignment = .fill
        }
    }()
    
    
    private lazy var redView = {
        return UIView().then { view in
            view.backgroundColor = .white
            view.glBoder(width: 1.0, color: .red)
        }
    }()
    
    private lazy var redShow = {
        return UILabel().then { view in
            view.setTitleAndColor("red" , color: .red)
        }
    }()
    
    private lazy var blueView = {
        return UIView().then { view in
            view.backgroundColor = .white
        }
    }()
    
    
    private lazy var blueShow = {
        return UILabel().then { view in
            view.setTitleAndColor("blue" ,  color: .blue)
        }
    }()
    
    override func initView(_ contentView: UIView) {
        linearLayout.addTo(contentView) { view in
            
            view.addArrangedSubview(redView)
            view.addArrangedSubview(blueView)
            
            view.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
        }
        
        redShow.addTo(redView) { view in
            view.snp.makeConstraints { make in
                make.width.equalToSuperview()
            }
        }
        
        
        blueShow.addTo(blueView) { view in
            view.snp.makeConstraints { make in
                make.width.equalToSuperview()
            }
        }
        
        input.addTo(blueView) { view in
            view.snp.makeConstraints { make in
                make.width.equalToSuperview()
                make.top.equalTo(blueShow.snp.bottom)
            }
        }
        
        timer.addTo(redView) { view in
            view.snp.makeConstraints { make in
                make.center.equalToSuperview()
            }
        }
    }
    
    lazy var input = {
        return UITextField().then { view in
            view.text = "111111111"
        }
    }()
    
    
    lazy var timer = {
        return UILabel().then { view in
            view.setTitleAndColor("开始计时")
        }
    }()
    
    
    override func bindViewModel(_ viewModel: AnyObserverAndBinderViewModel) {
        // 创建一个输入框
        input.rx.text.changed.subscribe { event in
            DDLogInfo(event)
        }.disposed(by: rx.disposeBag)
        
        let output = viewModel.transform(AnyObserverAndBinderViewModel.Input())

        // 设置一个秒表
        output.timer.bind { [weak self ]value in
            self?.timer.setTitleAndColor(value)
        }.disposed(by: viewModel.rx.disposeBag)
    }
    
}

class AnyObserverAndBinderViewModel : MVVMBaseViewModel , MVVMTransform {
    
    struct Input {
        
    }
    
    struct Output {
        let timer : Observable<String>
    }
    
    func transform(_ input: Input) -> Output {
        return Output(timer: Observable<Int>.interval(.seconds(1), scheduler: MainScheduler.instance).map { v -> String in
            return "当前计数 : \(v)"
        })
    }
    
}
